package com.bnyy.medicalHousekeeper.request;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.bnyy.medicalHousekeeper.base.App;
import com.bnyy.common.ResponseData;
import com.google.gson.Gson;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.scwang.smart.refresh.layout.api.RefreshLayout;

public abstract class BaseObserverImpl<T> extends BaseObserver<T> {

    private static final String TAG = "BaseObserverImpl";

    private Gson gson = new Gson();
    //    private ProgressDialog progressDialog;
    private RefreshLayout refreshLayout;
    private BasePopupView basePopupView;
    private Context context;
    private Runnable runnable;
    public BaseObserverImpl() {
        if (getContext() != null) {
            basePopupView = new XPopup.Builder(getContext())
                    .asLoading()
                    .show();
        } else {
            if (getRefreshLayout() != null) {
                refreshLayout = getRefreshLayout();
            }
        }
    }

    public BaseObserverImpl(RefreshLayout refreshLayout) {
        if (refreshLayout != null) {
            this.refreshLayout = refreshLayout;
        }
    }

    public BaseObserverImpl(Context context, Runnable runnable) {
        if (context != null) {
            this.runnable = runnable;
            basePopupView = new XPopup.Builder(context)
                    .asLoading()
                    .show();
//            progressDialog = CountDownProgressDialog.show(context, "", "正在请求服务器，请稍后...");
        }

    }

    public BaseObserverImpl(Context context) {
        if (context != null) {
            basePopupView = new XPopup.Builder(context)
                    .asLoading()
                    .show();
//            progressDialog = CountDownProgressDialog.show(context, "", "正在请求服务器，请稍后...");
        }

    }

    public BaseObserverImpl(Context context, String content) {
        if (context != null)
            basePopupView = new XPopup.Builder(context)
                    .asLoading()
                    .setTitle(content)
                    .show();
    }

    public BaseObserverImpl(Context context, String content, long countDonw) {
        if (context != null)
            basePopupView = new XPopup.Builder(context)
                    .asLoading()
                    .setTitle(content)
                    .show();
    }

    public RefreshLayout getRefreshLayout() {
        return refreshLayout;
    }

    public Context getContext() {
        return context;
    }

    @Override
    public void onSuccess() {
        Log.e(TAG, "onSuccess with no data");
    }

    @Override
    public void onOtherCode(int code) {
        Log.e(TAG, "onOrderCode");
    }

    @Override
    public void onOtherCode(int code, String msg) {
        Log.e(TAG, "onOrderCode code = " + code + " msg = " + msg);
    }

    @Override
    public void onSuccess(T t) {
        if (t == null) {
            Log.e(TAG, "onSuccess null");
        } else {
            Log.e(TAG, "onSuccess " + t.getClass().getSimpleName() + " = " + gson.toJson(t));
        }
    }

    @Override
    public void relogin() {
        Log.e(TAG, "relogin");
        // 登录失效，跳转到登录界面
        App.loginInvalid();
    }

    @Override
    public void showMessage(String msg) {
        Log.e(TAG, "showMessage msg = " + msg);
        if (!TextUtils.isEmpty(msg)) {
            Toast.makeText(App.getContext(), msg, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onResponse(ResponseData<T> responseData) {
        Log.e(TAG, "onResponse");
        finish();
    }

    @Override
    public void onCompleted() {
        Log.e(TAG, "onCompleted");
    }

    @Override
    public void onError(Throwable e) {
        finish();

        e.printStackTrace();
        Log.e(TAG, "onError = " + e.getMessage());

//        Utils.showToast(e.getMessage());
    }

    private void finish() {
        if (basePopupView != null && basePopupView.isShow()) {
            if (runnable == null) {
                basePopupView.delayDismiss(200);
            } else {
                basePopupView.delayDismissWith(200, runnable);
            }
        }
        if (refreshLayout != null) {
            if (refreshLayout.isRefreshing()) {
                refreshLayout.finishRefresh();
            }
            if (refreshLayout.isLoading()) {
                refreshLayout.finishLoadMore();
            }
        }
    }

    @Override
    public void onNext(T t) {

    }
}
